#!/bin/bash

source tools/getenv.sh
#echo $env
validCurrDirIsLink

echo -e "$GREEN 开始编译 go代码... $RESET"
if [[ "$env" == "dev" ]]; then
  echo -e "执行$GREEN dev $RESET 才会有的命令 go mod vendor 、 go generate ./... "
  echo "先后顺序必须是go mod vendor  然后 go generate ./..."
  go mod vendor
  if [[ $? -ne 0 ]]; then
    echo -e "${BOLODRED}go mod vendor失败,请查看上面的详细错误输出$RESET"
    exit 1
  fi
  go generate ./...
  if [[ $? -ne 0 ]]; then
    echo -e "${BOLODRED}go generate 失败,请查看上面的详细错误输出$RESET"
    exit 1
  fi
elif [[ "$env" == "test" ]]; then
  echo "test 不执行额外的编译"
elif [[ "$env" == "prod" ]]; then
  echo "prod 不执行额外的编译"
else
  echo  -e "${BOLODRED}无法判断环境，配置文件请包含dev|local,test,prod字样$RESET"
  exit 2
fi

echo "go build -o ${APP_BIN_NAME} main.go"
go build -o "${APP_BIN_NAME}" main.go
if [[ $? -ne 0 ]]; then
  echo -e "${BOLODRED}编译失败,请查看上面的详细错误输出$RESET"
  exit 1
fi

echo "编译完成"
